<?php

namespace App\Domain\Material\Requests;

use App\Domain\Material\Dao\MaterialDao;
use Illuminate\Foundation\Http\FormRequest;

class DistributionPost extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'SN' => ["required", function ($attribute, $value, $fail) {
                if ($value != '-1') {
                    $SNs = explode(',', $value);
                    foreach ($SNs as $v) {
                         $role = (new MaterialDao())::findBySNsAndDistributionStatus($v);
                    }
                    if ($role) {
                        $fail("SN已铺货或不存在");
                    }
                }
            }],
            'signatory' => 'string|nullable',
            'sign_time' => 'string|nullable',
            'channel_business_id' => 'string|nullable',
            'model' => 'required',
            'num' => 'required',

        ];
    }

    public function messages()
    {
        return [
            'SN.required' => 'SN序列号必填',
            'model.required' => '规格/型号必填',
            'num.required' => '数量必填',
        ];
    }
}
